package cn.afactory.tygz.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.utils.SPUtils;


/**
 * Created with Android Studio.
 * Title:RegProtocolActivity
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/9/2
 * Time:下午5:35
 * Version 1.0
 */
@ContentView(R.layout.ac_reg_protocol)
public class RegProtocolActivity extends BaseActivity {
    private String mUrl;
    private String mTitle;
    @ViewInject(R.id.wv_content)
    private WebView mWebView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initData();
        initBar();
    }

    /**
     * 打开内部浏览器
     * @param url
     */
    public static void openBrowser(Context context,String url){
        Intent intent = new Intent(context,RegisterActivity.class);
        intent.putExtra(SPUtils.EXTRA_DATA, url);
        context.startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        String url = intent.getStringExtra("web_url");
        if(null != url && TextUtils.isEmpty(url) ){
            mUrl = url;
            LogUtils.d("onNewIntent  to load url : " + url);
        }
    }

    /**
     * @Description 加载数据
     * @author xiezhidong251@jk.cn
     */

    private void initData() {
        Intent intent = getIntent();
        mTitle = getPageTitle();
        mUrl = intent.getStringExtra(SPUtils.EXTRA_DATA);
        WebSettings settings = mWebView.getSettings();
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setJavaScriptEnabled(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB) {
            settings.setDisplayZoomControls(false);
        }
        // 不使用缓存，只从网络获取数据
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.setWebViewClient(new WebViewClient(){

        });

        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitleText(title);
            }
        });
        if(mUrl != null){
            mWebView.loadUrl(mUrl);
        }else{
            mWebView.loadUrl("file:///android_asset/register_protocal.html");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(mWebView.canGoBack()){
                mWebView.goBack();//返回上一页面
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * @Description 初始化title和返回按钮
     * @author xiezhidong251@jk.cn
     */

    private void initBar() {
        if (mTitle != null) {
            setTitleText(mTitle);
        } else {
            setTitleText("服务条款");
        }
    }

    protected String getPageTitle() {
        return null == getIntent() ? "" : getIntent().getStringExtra("title");
    }

    /**
     * 分享打点参数(由各子类子控制具体参数)
     * @return
     */
    protected String getAgentParam(){
        return "ceping";
    }

    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }
}
